
  $(function () {

    init();
    function init() {
      getGoodsList();
      eventList();
    }

    function eventList() {
      $(".goods_content").on("click", ".btn_add", function () {
        let goods = $(this).parents(".goods_item").data("goods");
        let params = {
          'account': 'xiaoming',
          'goodsid': goods.id,
          'goodsname': goods.name,
          'price': goods.price,
          'num': 1
        }
        goodsAdd(params, function (res) {
          console.log(res);
          if(res){
            alert("成功");
          }

        })
      })
    }

    function goodsAdd(goods, success) {
      console.log(goods);
      $.ajax({
        url: "http://127.0.0.1:8092/mt/cart/add",
        type: "POST",
        success: success,
        data:goods
      })

    }

    function getGoodsList() {
      $.ajax({
        url: "http://127.0.0.1:8092/mt/goods/list",
        success(res) {
          let html = '';
          res.forEach(v => {
            html += ` <div class="goods_item"  data-goods='${JSON.stringify(v)}' > <img class="goods_img" src="./goods/${v.picture}" alt=""> <div class="goods_info_row"> <div class="goods_name">${v.name}</div> <div class="goods_price">${v.price}元/份</div> </div> <div class="month_sell">月销${v.month}单</div> <div class="send_price_row"> <span>起送￥10</span> <span>免配送费</span> <span>45分钟</span> </div> <div class="goods_tool"> <button class="btn_add">加入购物车</button> </div> </div> `;
          });
          $(".goods_content").html(html);
        }
      })


    }

  })
